#include "qkeyboardbuttonhost.h"

#include "qabstractproperty.h"
#include "qpropertyfactory.h"
#include "qcommonstruct.h"

#include "qrenamedialog.h"

#include <QAction>

using namespace SHAREDLIB;

QKeyboardButtonHost::QKeyboardButtonHost(QObject *object) :
    QAbstractHost(object)
{
    QAbstractProperty *pro;
    pro=QPropertyFactory::createProperty("Name");
    if(pro!=NULL)
    {
        pro->setAttribute("name","objectName");
        pro->setAttribute("property_name",tr("Name"));
        pro->setAttribute("group","Attributes");
        pro->setAttribute("NeedSave",true);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("ByteArray");
    if(pro!=NULL)
    {
        pro->setAttribute("name","text");
        pro->setAttribute("property_name",tr("Key"));
        pro->setAttribute("group","Attributes");
        pro->setAttribute("NeedSave",true);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","clicked");
        pro->setAttribute("property_name",tr("Clicked"));
        pro->setAttribute("group","Events");
        m_object->setProperty("clicked","");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","capslock_changed");
        pro->setAttribute("property_name",tr("Capslock Changed"));
        pro->setAttribute("group","Events");
        m_object->setProperty("clicked","");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Rect");
    if(pro!=NULL)
    {
        pro->setAttribute("name","geometry");
        pro->setAttribute("property_name",tr("Geometry"));
        pro->setAttribute("group","Attributes");
        pro->setAttribute("NeedSave",true);
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Bool");
    if(pro!=NULL)
    {
        pro->setAttribute("name","enabled");
        pro->setAttribute("property_name",tr("Enabled"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Font");
    if(pro!=NULL)
    {
        pro->setAttribute("name","font");
        pro->setAttribute("property_name",tr("Font"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Cursor");
    if(pro!=NULL)
    {
        pro->setAttribute("name","cursor");
        pro->setAttribute("property_name",tr("Cursor"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Stylesheet");
    if(pro!=NULL)
    {
        pro->setAttribute("name","background");
        pro->setAttribute("property_name",tr("Background"));
        pro->setAttribute("group","Style Sheet");

        StyleItems items;
        tagStyleItem item;

        item.m_string="normal";
        item.m_text=tr("Normal");
        items.append(item);


        item.m_string="hover";
        item.m_text=tr("Hover");
        items.append(item);

        QVariant v;
        v.setValue<StyleItems>(items);
        pro->setAttribute("items",v);

        m_object->setProperty("background",QVariant());
        addProperty(pro);
    }

    pro=SHAREDLIB::QPropertyFactory::createProperty("Stylesheet");
    if(pro!=NULL)
    {
        pro->setAttribute("name","border");
        pro->setAttribute("property_name",tr("Border"));
        pro->setAttribute("group","Style Sheet");

        StyleItems items;
        tagStyleItem item;

        item.m_string="normal";
        item.m_text=tr("Normal");
        items.append(item);


        item.m_string="hover";
        item.m_text=tr("Hover");
        items.append(item);

        QVariant v;
        v.setValue<StyleItems>(items);
        pro->setAttribute("items",v);

        m_object->setProperty("background",QVariant());
        addProperty(pro);
    }


    QAction* ac=new QAction(QIcon(),tr("Edit"),NULL);
    addAction("edit",ac);
    connect(ac,SIGNAL(triggered()),this,SLOT(edit_slots()));
}

void QKeyboardButtonHost::setText(const QString &text)
{
    m_object->setProperty("text",text);
}

QString QKeyboardButtonHost::text()
{
    return m_object->property("text").toString();
}

void QKeyboardButtonHost::key_char(const QString &text)
{
    QMap<QString,QString>   param;
    param.insert("text",text);
    emit_exec("keyboard_send(1,text);",param);
}

void QKeyboardButtonHost::key_capslock()
{
    QMap<QString,QString>   param;
    param.insert("text","");
    emit_exec("keyboard_send(16777252,text);",param);
}


void QKeyboardButtonHost::edit_slots()
{
    QRenameDialog dialog((QWidget*)m_object);

    dialog.set_old_name(m_object->property("objectName").toString());

    QStringList list;
    if(m_parent!=NULL)
    {
        foreach(QAbstractHost *h,m_parent->getChildren())
        {
            if(h!=this)
            {
                list.append(h->getPropertyValue("objectName").toString());
            }
        }
    }
    dialog.set_name_list(list);

    dialog.exec();

    if(dialog.get_ret()==1)
    {
        m_object->setObjectName(dialog.get_new_name());
    }
}

